package com.wuyong.chapter8;

public class Array<E> {

    private int size;
    private E[] data;

    public Array(int capacity) {
        data = (E[]) new Object[capacity];
        size = 0;
    }
    public Array(E[] arr) {
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
        size = arr.length;
    }

    public Array() {
        this(10);
    }

    public int getSize() {
        return size;
    }

    public int getCapacity() {
        return data.length;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public E get(int index) {
        if (index >  size || index < 0) {
            throw new IllegalArgumentException("insert fail, index illegal");
        }
        return data[index];
    }

    public void set(int index, E e) {
        if (index >  data.length || index < 0) {
            throw new IllegalArgumentException("insert fail, index illegal");
        }
         data[index] = e;
    }

    public void addLast(E e) {
        if (size ==  data.length) {
            throw new IllegalArgumentException("add e fail, th data is full");
        }
        data[size] = e;
        size++;
    }

    public void addFirst(E e) {
        add(0, e);
    }

    public void insert(E e, int index) {
        if (size ==  data.length) {
            throw new IllegalArgumentException("add e fail, the data is full");
        }
        if (index >  size || index < 0) {
            throw new IllegalArgumentException("insert fail, index acquire not satisfacty");
        }
        for (int i = size; i > index; i--) {
            data[i] = data[i - 1];
        }
        data[index] = e;
        size++;
    }

    // 在index索引的位置插入一个新元素e
    public void add(int index, E e){

        if(index < 0 || index > size)
            throw new IllegalArgumentException("Add failed. Require index >= 0 and index <= size.");

        if(size == data.length)
            resize(2 * data.length);

        for(int i = size - 1; i >= index ; i --)
            data[i + 1] = data[i];

        data[index] = e;

        size ++;
    }

    // 查找数组中元素e所在的索引，如果不存在元素e，则返回-1
    public int find(E e){
        for(int i = 0 ; i < size ; i ++){
            if(data[i].equals(e))
                return i;
        }
        return -1;
    }

    // 从数组中删除元素e
    public void removeElement(E e){
        int index = find(e);
        if(index != -1)
            remove(index);
    }

    public boolean contain(E e) {
        for (E x : data) {
            if (x.equals(e)) {
                return true;
            }
        }
        return false;
    }

    public void swap(int i , int j) {
        if(i < 0 || i >= size || j < 0 || j >= size)
            throw new IllegalArgumentException("args illegal.");

        E e = data[i];
        data[i] = data[j];
        data[j] = e;

    }

    // 从数组中删除index位置的元素, 返回删除的元素
    public E remove(int index){
        if(index < 0 || index >= size)
            throw new IllegalArgumentException("Remove failed. Index is illegal.");

        E ret = data[index];
        for(int i = index + 1 ; i < size ; i ++)
            data[i - 1] = data[i];
        size --;
        data[size] = null; // loitering objects != memory leak

        if(size == data.length / 4 && data.length / 2 != 0)
            resize(data.length / 2);
        return ret;
    }

    @Override
    public String toString() {
        StringBuilder result = new StringBuilder();
        result.append(String.format("Array size is %d, capacity is %d .", size, data.length));
        result.append("[");
        for (int i = 0; i < size; i++) {
            result.append(data[i]);
            if (i != size - 1) {
                result.append(",");
            }
        }
        result.append("]");
        return result.toString();
    }

    private void resize(int newLen) {
        E[] newData = (E[]) new Object[newLen];
        for (int i = 0; i < data.length; i++) {
            newData[i] = data[i];
        }
        data = newData;
    }
}
